0% found this document useful (0 votes)
16 views10 pages

WeChat Design Patterns Overview

Design Pattern in software engineering_singleton, adapter, observer

Uploaded by

Corona Corona
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

WeChat Design Patterns Overview

Design Pattern in software engineering_singleton, adapter, observer

Uploaded by

Corona Corona
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Design Pattern Questions for Practice

Question 1 [Python Solution can be found here]


Let’s consider a situation, where you are a pro-level software engineer. Now, you are being hired
by Twitter. At this moment, Twitter wants to develop a website where they want to create a small
version of [Link], since it is a small version of YouTube, thus they want you to make
sure that it has only one channel which is named “Twitter”. They want you to ensure that no
other channel can be made within this newly developed YouTube Platform. They want you to
make sure that the site will be able to notify its users once a video is released. Now, your job is
to choose suitable design pattern that can help to develop the software. [8]

Question 2 [Python Solution can be found here]


Let’s consider a situation, where you are a pro-level software engineer. Now, you are being hired
by Twitter. At this moment, Twitter wants to develop a website where they want to create a small
version of YouTube. However, it can have multiple channels like normal YouTube. Now, the new
version of your developed YouTube will have to have a channel named Twitter. That will post
similar videos, reels/shorts like Linux-Tech-Tips. Also, users will get notified for the newly
uploaded videos. Now, your job is to choose suitable design pattern that can help to develop the
software. [8]

Question 3
Let’s consider a situation where you want to develop software called WECHAT that will have a
chatting option like Messenger, news feed, and friends group option like Facebook and a
streaming option like Twitch. Now, in order to complete the process, you also hired some new
people who can work with you in this project. You want to make sure that there will be only one
Application that can solve the problem of All the mentioned Applications. Now, for Facebook,
you can implement the feature called newsfeed and groups. For Messenger, you can only
implement the feature of Chatting. In addition, for Twitch you can develop feature like video
streaming. Now, your job is to choose suitable design patterns that can help to develop the
software. [7]

//Driver Code JAVA version //Driver Code Python version

WECHAT weChat = [Link]( ); weChat =WECHAT. getInstance( );


[Link]( ); [Link]( );
WECHAT weChat2 =[Link]( ) ; weChat2 =WECHAT. getInstance( ) ;
[Link]( ); [Link]( );
[Link]( ); [Link]( );
[Link]( ); [Link]( );
[Link]. println(weChat); print(weChat);
[Link]. println(weChat2); print(weChat2);

Question 4

In Tech-Park, "Magic Sweets" was famous for its magical cakes, crafted solely by Mr. Shaheed,
the master baker. His secret was consistency—each cake tasted exactly like the first, and only he
was allowed to make them to maintain this perfect standard. When Jamie, a new baker,
suggested allowing others to help increase production, Mr. Shaheed explained, “Our success
comes from having one source—the master baker—ensuring every cake is consistently perfect,
just like the first.” Inspired, Jamie applied this idea to their software system by implementing a
design pattern for configuration settings. Just as Mr. Shaheed’s approach kept the cakes
consistent, the design pattern ensured the class maintained stable and controlled settings.[7]
Question 1 Answer: Singleton, Observer

import [Link];
import [Link];

public class Youtube {

private List<Subscriber> subscribersList= new ArrayList<Subscriber>();


public static List <String> videos = new ArrayList<String>();
public String channelName = "Twitter";
private static final Youtube Twitter = new Youtube();

// privatization constructor to stop creating multiple object


private Youtube(){ }

public void subscribe(Subscriber s)


{ //Adding subscriber to subscribe list
[Link](s);
[Link] = this;
}
public void notification(){
//notify the subscriber
for(int i=0; i<[Link](); i++)
{
[Link](subscribersList[i]);
}
}

public static Youtube provideObject( )


{ //returning single object
return Twitter;
}
public void contentUpload(String content)
{
[Link](content);
[Link]();
}
}
public class Subscriber {

public Youtube channel;


public String userName;

public Subscriber(String user)


{
userName = user;
}

public void update(Youtube c)


{
[Link]("hey " + userName + "your subscribe channel "
+ [Link] + " uploaded new video");
for(int i=0; i<[Link](); i++)
{
if( i== [Link]()-1)
{
[Link]("Video Name"+ [Link](i));
}
}
}

public void subscribeTwitter( )


{
Youtube channel = [Link]( );
[Link](this);
}

/////////////////////////////// Driver Code ////////////////////////////////

public class Main {


public static void main(String[] args) {
////////////////////// Driver Code //////////////////////////
Subscriber Subscriber1 = new Subscriber ("Lionel Messi");
[Link]();
Youtube twitter = [Link]();
[Link]("New Video 1");
[Link]("New Video 2");
[Link]("New Video 4");
[Link]("New Video 6");

[Link](twitter);
}
}
Question 2 Answer: Adapter, Observer

public interface ChannelInterface {

public void uploadContent(String contentName);


}

public class LinusChannel {

public void postFunnyVideo()


{
[Link]("Linus channel video uploaded on Nvidia gpu");
}
}

public class Youtube extends LinusChannel implements ChannelInterface {

private List<Subscriber> subscribersList = new ArrayList();

public static List<String> videos = new ArrayList();

public String channelName;

public Youtube(String channelName) {


[Link] = channelName;
}

public void subscribe(Subscriber s) {


[Link](s);
[Link] = this;
}

public void notification(){


for(int i=0; i<[Link](); i++)
{
[Link](subscribersList[i]);
}
}

public void uploadContent(String contentName) {


[Link]();
[Link](contentName);
[Link]();
}
}
public class Subscriber {

public Youtube channel;


public String userName;

public Subscriber(String user)


{
userName = user;
}

public void update(Youtube c)


{
[Link]("hey " + userName + "your subscribe channel "
+ [Link] + " uploaded new video");
for(int i=0; i<[Link](); i++)
{
if( i== [Link]()-1)//notify About latest video
{
[Link]("Video Name"+ [Link](i));
}
}

public void subscribeTwitter( )


{
Youtube channel = [Link]( );
[Link](this);
}

//////////////////////////////Driver Code ////////////////////////////


public class Main {
public static void main(String[] args) {
Subscriber Subscriber1 = new Subscriber ("Ching Chong");
[Link]();
Youtube twitter = [Link]();
[Link]("New Video 1");
[Link]("New Video 2");
[Link]("New Video 4");
[Link]("New Video 6");
//[Link]();
[Link](twitter);

}
}
Question 3 Answer: Adapter, Singleton
public class WECHAT {

private static final WECHAT weChat = new WECHAT();


public facebook facebook = new facebook();
public twitch twitch = new twitch();
public messenger messenger = new messenger();

public static WECHAT getInstance( )


{
return weChat;
}

public void sendMessage()


{
[Link]("We chat has it's own message to send");
[Link]();
}

public void useNewsFeed()


{
[Link]("Let's use the NewFeed");
[Link]();
}

public void streamingVideo()


{
[Link]("Let's stream videos");
[Link]();
}

public void createFriendsGroup()


{
[Link]("Let's create friends group");
[Link]();
}
}

public class facebook {


public void createFriendsGroup()
{
[Link]("Welcome to the group");
}
public void useNewFeed()
{
[Link]("Scroll through news feed");
}
}
public class messenger {
public void sendMessage()
{
[Link]("Let's Message each other");
}
}

public class twitch {


public void streamingVideo()
{
[Link]("Videos from twitch");
}
}

//////////////////////////Driver Code /////////////////////////////


public class Main {
public static void main(String[] args) {

WECHAT weChat = [Link]();


[Link]();
WECHAT weChat2 = [Link]();
[Link]();
[Link]();
[Link](weChat);
[Link](weChat2);
}
}
Question 4 Answer: Singleton

////////////////////////////////Driver Code ////////////////////////////////

public class Main {


public static void main(String[] args) {

magicalCake cake1 = [Link]();


magicalCake cake2 = [Link]();
magicalCake cake4 = [Link]();
magicalCake cake6 = [Link]();

[Link](cake1);
[Link](cake2);
[Link](cake4);
[Link](cake6);
}
}

public class magicalCake {

private static final magicalCake MagicalCake = new magicalCake();

private magicalCake(){}

public static magicalCake prepareMagicalCake()


{
return MagicalCake;
}
}

Output : Same obj ref

Common questions

Powered by AI

The Observer pattern allows the system to efficiently manage and scale by pushing updates such as video uploads to all subscribers without direct coupling between the components, enhancing scalability. The Singleton pattern ensures there is only a single instance managing these updates, providing a consistent and controlled user experience .

The Singleton pattern should be used to ensure that only one channel named "Twitter" can exist. This pattern restricts instantiation of a class to a single object, so no other channels can be created in the YouTube platform . The use of the Observer pattern will also help notify users when a new video is released .

The Singleton pattern should be applied to ensure there is only one instance of the WECHAT application managing all these functionalities. The Adapter pattern is also necessary to allow WECHAT to conform to the interfaces of Facebook, Messenger, and Twitch, incorporating their functionalities without altering their structures .

The Singleton pattern, like having a 'master baker', ensures that there is a single point of control and consistency in the objects created by an application. Just as a master baker ensures all cakes have the same quality by being the sole person baking them, a Singleton restricts class instantiation and ensures uniformity across the application using a single instance .

Allowing multiple instances could lead to inconsistency in data, complicated state management, and increased resource consumption. The Singleton pattern addresses these challenges by ensuring only one instance of the platform operates, maintaining consistency, easy state management, and optimal resource utilization .

The Adapter pattern can be used alongside the Observer pattern. The Adapter pattern allows objects with incompatible interfaces to work together, which would enable the integration of a specific channel like Twitter that acts similarly to Linux-Tech-Tips . The Observer pattern will handle user notifications for new video uploads .

Using both the Adapter and Singleton patterns together allows WECHAT to function as a versatile platform that can integrate functionalities from other applications like Facebook, Messenger, and Twitch, maintaining those functionalities through a single, consistent interface (Singleton) while adapting the different components to work together seamlessly despite differing interfaces (Adapter).

A Singleton pattern can ensure consistency by maintaining a single point of control over the instantiation of an object, similar to Mr. Shaheed's approach. By having a single source for the configuration settings, it ensures that all parts of the system are using consistent settings .

The benefits of using the Singleton pattern include controlled access to the sole instance, reduced memory footprint, and consistency throughout the application. The drawbacks can include challenges in unit testing due to its global state, potential issues with scalability, and making the system more prone to bottlenecks since all operations are passed through a single instance .

Mr. Shaheed's principle represents the importance of having a single, controlled source for maintaining consistency and quality, akin to using the Singleton pattern in application development. By having a single instance controlling certain aspects of the application, consistency across different components can be achieved, similar to how Mr. Shaheed ensures all cakes are consistent in quality .

You might also like