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